﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace SPSolutions.Samples.RibbonApp.Layouts.RibbonApp
{
    /// <summary>
    /// Represent a ribbon page
    /// </summary>
    public class RibbonPageBase : LayoutsPageBase
	{
        /// <summary>
        /// Get the cancel script with a return path
        /// </summary>
        protected string GetCancelScript(string returnPath, bool useSource)
        {
            SPRedirectFlags flags = SPRedirectFlags.DoNotEncodeUrl | SPRedirectFlags.Static;
            if (useSource)
            {
                flags |= SPRedirectFlags.UseSource;
            }

            string urlRedirect = string.Empty;
            SPUtility.DetermineRedirectUrl(returnPath, flags, this.Context, null, out urlRedirect);

            if (base.IsDialogMode)
            {
                return "window.frameElement.cancelPopUp();return false";
            }
            else
            {
                return String.Format(
                    "window.location='{0}';return false",
                    Microsoft.SharePoint.Utilities.SPEncode.ScriptEncode(urlRedirect));
            }
        }

        /// <summary>
        /// Set the page status message
        /// </summary>
        /// <param name="statusHtml"></param>
        public void SetStatusMessage(string statusHtml)
        {
            string script = string.Empty;

            if (!string.IsNullOrEmpty(statusHtml))
            {
                script = 
                    "<script type=\"text/javascript\">\r\n" +
                    "//<![CDATA[\r\n" +
                    "    addStatus('', '" + SPHttpUtility.EcmaScriptStringLiteralEncode(statusHtml.Trim()) + "', false);\n" +
                    "    setStatusBarClass('s4-status-s3');\r\n" +
                    "//]]>\r\n" +
                    "</script>";
            }
            else
            {
                script = 
                    "<script type=\"text/javascript\">\r\n" +
                    "//<![CDATA[\r\n" +
                    "    removeAllStatus(true);\r\n" +
                    "//]]>\r\n" + 
                    "</script>";
            }
            this.Page.ClientScript.RegisterStartupScript(typeof(SPRibbon), "statusmessage", script, false);
        }

        /// <summary>
        /// Redirect the the users page
        /// </summary>
        /// <param name="useSource"></param>
        protected void RedirectToPage(string pageUrl, bool useSource)
        {
            SPRedirectFlags flags = SPRedirectFlags.DoNotEncodeUrl | SPRedirectFlags.Static;
            if (useSource)
            {
                flags |= SPRedirectFlags.UseSource;
            }
            if (base.IsDialogMode)
            {
                string urlRedirect = string.Empty;
                SPUtility.DetermineRedirectUrl(pageUrl, flags, this.Context, null, out urlRedirect);
                this.Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('" + urlRedirect + "');</script>");
                this.Context.Response.Flush();
                this.Context.Response.End();
            }
            else
            {
                SPUtility.Redirect(pageUrl, flags, this.Context);
            }
        }
	}
}
